package com.example.utils;

import javax.servlet.http.HttpServletResponse;

/**
 * WebUtils工具类
 * 将字符串渲染到客户端，向响应之中写入聚类
 *
 * @author ding
 * @since 2024/7/23
 */
public class WebUtils {

    /**
     * 将字符串渲染到客户端
     * @param response  渲染对象
     * @param string    待渲染的字符串
     */
    public static String renderString(HttpServletResponse response, String string){
        try{
            response.setStatus(200);
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().print(string);
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static String renderString403(HttpServletResponse response, String string){
        try{
            response.setStatus(403);
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().print(string);
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

    public static String renderString401(HttpServletResponse response, String string){
        try{
            response.setStatus(401);
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().print(string);
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }


}
